<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索查询 -->
      <el-row :gutter="20">
        <el-col :span="9">
          <div style="display: flex !important;">
            <el-input placeholder="请输入标题" v-model="queryParam.title" style="width: 200px;margin-right: 5px;">
              <template slot="prepend">标题</template>
            </el-input>
            <el-input placeholder="" v-model="queryParam.nickname" style="width: 210px;">
              <template slot="prepend">作者昵称</template>
            </el-input>
          </div>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="handleSizeChange(10)">查询</el-button>
          <el-button type="primary" @click="updateOrCreateArticleClick('add')">添加文章</el-button>
        </el-col>
      </el-row>
      <!-- table表格数据 start -->
      <template>
        <el-table :data="articleDataList" style="width: 100%" max-height="570">
          <!-- 数据表 -->
          <el-table-column fixed prop="title" label="标题" width="150">
          </el-table-column>
          <el-table-column prop="describe" label="描述" width="300">
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="150">
          </el-table-column>
          <el-table-column prop="user" label="作者" width="80">
          </el-table-column>
          <el-table-column prop="cover_path" label="图片" width="120">
            <template slot-scope="scope">
              <div class="demo-image__preview">
                <el-image style="width: 100px; height: 100px" :src="scope.row.thumbnail_path" :preview-src-list="[scope.row.thumbnail_path]">
                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="80">
            <template slot-scope="scope">
              <el-switch @change="statusChange(scope.row)" v-model="scope.row.status" active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" width="100">
          </el-table-column>
          <el-table-column prop="like_count" label="点赞数" width="70">
          </el-table-column>
          <el-table-column prop="brown_count" label="浏览数" width="70">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <!-- 操作 -->
            <template slot-scope="scope">
              <!-- <el-button size="mini" type="warning" icon="el-icon-view" circle></el-button> -->
              <el-button size="mini" type="warning" icon="el-icon-edit" @click="updateOrCreateArticleClick(scope.row.id)" circle></el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteArticle(scope.$index,scope.row.id)"
                circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- table表格数据 end -->
      <!-- 分页  start-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParam.currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="totalResult">
      </el-pagination>
      <!-- 分页  end> -->
    </el-card>
    <!-- 表单 end -->
  </div>
</template>

<script>
  import {
    _getArticleListApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        articleDataList: [],
        totalResult: 0,
        queryParam: {
          tags: '',
          nickname: '',
          title: '',
          currentPage: 1,
          pageSize: 10
        }
      }
    },
    created() {
      this.getArticleList()
    },
    methods: {
      // 获取列表数据
      async getArticleList() {
        // 获取分类列表
        const {
          data: res
        } = await _getArticleListApi(this.queryParam)
        console.log(res)
        this.articleDataList = res.data.dataList
        this.totalResult = res.data.totalResult
      },
      // 状态改变
      async statusChange(article) {
        // 状态更新
        const {
          data: res
        } = await this.$http.put('article/' + article.id + '/', article)
        if (res.status) {
          this.$message.success('修改成功！')
        } else {
          this.$message.error('操作失败！')
        }
      },
      async deleteArticle(index, id) {
        // 删除
        const data = await this.$http.delete('article/' + id + '/')
        if (data.status === 200 && data.data.status) {
          this.$message.success('删除成功！')
          this.articleDataList.splice(index, 1)
        }
      },
      editArticle(articleId) {
        // 跳转到编辑页
        this.$router.push({
          name: 'write_article',
          query: {
            articleId: articleId
          }
        })
      },
      searchArticle() {
        this.queryParam.currentPage = 1
        this.getArticleList()
      },
      updateOrCreateArticleClick(articleId) {
        // 跳转到编辑页
        this.$router.push({
          name: 'write_article',
          query: {
            articleId: articleId
          }
        })
      },
      // 分页事件
      handleSizeChange(newSize) {
        this.queryParam.pageSize = newSize
        this.queryParam.currentPage = 1
        this.getArticleList()
      },
      handleCurrentChange(newPage) {
        this.queryParam.currentPage = newPage
        this.getArticleList()
      }
    }
  }
</script>

<style>
  .el-input-group__prepend {
    padding: 0 15px !important;
  }
</style>
